package com.jia.lidao.ui.login;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.jia.lidao.App;
import com.jia.lidao.R;
import com.jia.lidao.RunningContext;
import com.jia.lidao.UserUtils;
import com.jia.lidao.enity.LoginRespDTO;
import com.jia.lidao.network.http.BusinessManager;
import com.jia.lidao.network.http.MyCallback;
import com.jia.lidao.network.http.request.impl.JpushBindRequest;
import com.jia.lidao.network.http.request.impl.LoginRequest;
import com.jia.lidao.ui.BaseActivity;
import com.jia.lidao.ui.personal.PersonalActivity;

import cn.jpush.android.api.JPushInterface;


/**
 * 模拟登录
 * <p>
 * Created by czhang on 17/12/15.
 */
public class LoginActivity extends BaseActivity implements View.OnClickListener{


    private ImageView ivFinish;
    private EditText etAccount;
    private EditText etPsw;
    private TextView tvLogin;
    private TextView tvRegister;
    private TextView tvForgetPsd;





    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        if(UserUtils.getInstance().getAuthor()!=null){
            startActivity(new Intent(LoginActivity.this,PersonalActivity.class));
            finish();
        }
        initView();
    }

    private void initView() {
        ivFinish=(ImageView)findViewById(R.id.iv_finish);
        etAccount=(EditText)findViewById(R.id.et_account);
        etPsw=(EditText)findViewById(R.id.et_psw);
        tvLogin=(TextView)findViewById(R.id.tv_login);
        tvRegister=(TextView)findViewById(R.id.tv_register);
        tvForgetPsd=(TextView)findViewById(R.id.tv_forget_psd);
        ivFinish.setVisibility(View.INVISIBLE);
        etAccount.setText(UserUtils.getInstance().getPhone());
        tvLogin.setOnClickListener(this);
        tvRegister.setOnClickListener(this);
        tvForgetPsd.setOnClickListener(this);
        ivFinish.setOnClickListener(this);
    }

    private void login() {
        final String phone = etAccount.getText().toString().trim();
        if (TextUtils.isEmpty(phone)) {
            showToast("请输入账号");
            return;
        }


        String pswd = etPsw.getText().toString().trim();
        if (TextUtils.isEmpty(pswd)) {
            showToast("请输入密码");
            return;
        }
        showDialog("");

        BusinessManager.getInstance().loginTel(
                new LoginRequest(phone, pswd),
                new MyCallback<LoginRespDTO>() {
                    @Override
                    public void onSuccess(LoginRespDTO result, String message) {
                        if (result == null || result.authorization == null) {
                            showResultErrorToast();
                            return;
                        }
                        dismissDialog();
                        UserUtils.getInstance().saveAuthor(result.authorization);
                        UserUtils.getInstance().savePhone(phone);
                        startActivity(new Intent(LoginActivity.this, PersonalActivity.class));
                        //jpush_import
                        BusinessManager.getInstance()
                                .bindJpush(new JpushBindRequest(JPushInterface
                                                .getRegistrationID(App.instance)),
                                        new MyCallback<Void>() {
                                            @Override
                                            public void onSuccess(Void result,
                                                                  String message) {

                                            }

                                            @Override
                                            public void onError(
                                                    String errorCode,
                                                    String message) {
                                            }

                                            @Override
                                            public void onFinished() {
                                            }

                                            @Override
                                            public void onRefreshToken() {

                                            }
                                        });
                        finish();


                    }

                    @Override
                    public void onError(String errorCode, String message) {
                        dismissDialog();
                        showToast(message);
                    }

                    @Override
                    public void onFinished() {

                    }

                    @Override
                    public void onRefreshToken() {

                    }
                });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv_login:
                login();
                break;
            case R.id.tv_forget_psd:
                Intent it = new Intent(LoginActivity.this, RegisterActivity.class);
                it.putExtra("isForgetPSWD", true);
                startActivity(it);
                break;
            case R.id.tv_register:
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
                break;
            case R.id.iv_finish:
                finish();
                break;
        }
    }
}
